<?php
class NegociosController 
	extends Zend_Controller_Action 
{

	public function links( $ciudad = null )
	{
	    $idCiudad = $ciudad ? $ciudad->getId(): null;
	    
		$this->view->mainRight = true;
		
		$mapper = Delivery_Model_Factory::mapper( 'Tags' );
		
		$dbTable1 = new Delivery_Model_DbTable_Negocios;
		$this->view->negociosPorCiudad = $dbTable1->countByCity();
		$this->view->negociosPorTag = $this->view->tags = $mapper->getLinked( $idCiudad );
	}
	
	public function ciudadAction()
	{
		$nombreCiudad = $this->_helper->varUrl( $this->_getParam('ciudad'));
		$nombreProvincia = $this->_helper->varUrl( $this->_getParam('provincia'));
		$tagName = $this->_helper->varUrl( $this->_getParam('tagName'));
		
		$mapperN = Delivery_Model_Factory::mapper( 'Negocios' );
		$mapperC = Delivery_Model_Factory::mapper( 'Ciudades' );
		
		$pagination = $this->getHelper('Pagination');
		
		
//		Zend_Debug::dump( $pagination );
//		exit;
		
		$ciudad = $mapperC->getByNombre( $nombreCiudad, $nombreProvincia);
		
		if( ! $ciudad ){
			return $this->_forward('not-found', 'error');
		}
		
		$this->links( $ciudad );
		
		$params = array('idCiudad' =>$ciudad->getId());$params = array('idCiudad' =>$ciudad->getId());
		
		if ( $tagName ){
		    
		    $mapperT = Delivery_Model_Factory::mapper( 'Tags' );
		    $tag = $mapperT->getByName( $tagName );
		    $this->view->tag = $tag;
		    $params = array('idTag' =>$tag->getId());
		    
		} 
		
//		die($pagination->order);
		
		$negocios = $mapperN->getByParams( $params, $pagination->offset, $pagination->limit, $pagination->order );
		$totalItems = $mapperN->countByParams( $params );
		
		$pagination->setItemsCount( $totalItems );
		
		
		$mapperN->loadTags( $negocios );
		$mapperN->loadDireccion( $negocios );
		$mapperN->loadHorarios( $negocios );
		
		
		$this->view->ciudad = $ciudad;
		$this->view->negocios = $negocios;		
		
	    if ( $this->getRequest()->isXmlHttpRequest()){
			$this->_helper->layout->disableLayout();
			$this->render('negocio');
		}
		
	}
		
	public function tagAction()
	{
		
		try {
		    $this->links();
		    
			$tagName = $this->_helper->varUrl( $this->_getParam('tagName'));
			$tagType = $this->_getParam('tagType');
			
			$mapper1 = Delivery_Model_Factory::mapper( 'Tags' );
			$mapper2 = Delivery_Model_Factory::mapper( 'Negocios' );
			
			$pagination = $this->getHelper('Pagination');
			
			$tag = $mapper1->getByTypeAndName( $tagType, $tagName );
			
			$params = array('idTag' =>$tag->getId());
			
			$negocios = $mapper2->getByParams( $params, $pagination->offset, $pagination->limit, $pagination->order );
    		$totalItems = $mapper2->countByParams( $params );
    		
    		$pagination->setItemsCount( $totalItems );
			
			$mapper2->loadTags( $negocios );
			$mapper2->loadDireccion( $negocios );
			$mapper2->loadHorarios( $negocios );
			
			$this->view->tag = $tag;
			$this->view->negocios = $negocios;
		
    		if ( $this->getRequest()->isXmlHttpRequest()){
    			$this->_helper->layout->disableLayout();
    			$this->render('negocio');
    		}
			
			
		} catch (Delivery_Model_Exception $e) {
			
			if( $e->getMessage() == Delivery_Model_Exception::TAG_NOT_FOUND ){
				
				return $this->_forward('not-found', 'error');
			} else {
				throw $e;
			}
		}
		
		
	}
	
	public function buscarAction()
	{
	    $this->links();
	    
		$where	= $this->_getParam('where');
		$what	= $this->_getParam('what');
		$limit	= (int) $this->_getParam('limit');
		$page	= (int) $this->_getParam('page', 1);
		$orderBy= $this->_getParam('orderBy');
		
		$logger = new Delivery_Model_Logger;
		
		$where	= $where != $this->defaultWhere ? $where : null;
		$what	= $what != $this->defaultWhat ? $what : null;
		$totalItems = 0;
		$stopSearch = false;
		
		$pagination = $this->getHelper('Pagination');
		
		
		$session= Zend_Registry::get('session');
		
		
		$direccion = null;
		$idDireccion = null;
		$idCiudad = null;
		
		$mapper1 = Delivery_Model_Factory::mapper( 'Direcciones' );
		$mapper2 = Delivery_Model_Factory::mapper( 'Negocios' );
		
		if ( ! empty($where)){	
			
			$direcciones = $mapper1->search($where);		
			
			$numAddress = count($direcciones);
			

			
			
			if ( $numAddress == 1 ){
				
				$direccion = current($direcciones);
				
				$idDireccion = $direccion->idDireccion;
				$session->where = $where;
				$this->view->where = $where;
				
				$this->view->direccion = $direccion;
				$session->idDireccion = $direccion->getId();
				
			} elseif ( $numAddress > 1 ) {
			
				$this->view->direccionesEncontradas = $direcciones;
				$stopSearch = true;
				
			} else {
			
				$this->getHelper('MyFlashMessenger')
					 ->addError( $this->_helper->Translate('cannotFindAddress') );
				
			}
		} else {
			unset($session->where);
			$this->view->where = $this->defaultWhere;
		}
		
		if ( !$what  && !$where){
			$this->getHelper('MyFlashMessenger')
				 ->addError( $this->_helper->Translate('searchParamsMissing') );
		} elseif ( !$stopSearch && ($what || $idDireccion)) {
		
			$this->view->negocios = $mapper2->find( $what, $idDireccion, $pagination->offset, $pagination->limit, $pagination->order);
			
		} else {
		
			$this->view->negocios = null;
			
		}
		
		if ($what){
			$session->what = $what;
			$this->view->what = $what;
		} else {
			unset($session->what);
			$this->view->what = $this->defaultWhat;
		}

		
		/*
		 * Paginator
		 */
		
		
		if( $this->view->negocios ){
		
			$totalItems = $mapper2->findCount( $what, $idDireccion, $pagination->offset, $pagination->limit, $pagination->order);
			
			$pagination->setItemsCount( $totalItems );
			
//			Zend_Paginator::setDefaultItemCountPerPage( $limit );
//		
//			$adapter = new Zend_Paginator_Adapter_Null($totalItems);
//			
//			$this->view->paginator = new Zend_Paginator($adapter);		
//			$this->view->paginator->setCurrentPageNumber( $page );
			
			$mapper2->loadTags( $this->view->negocios );
			$mapper2->loadDireccion( $this->view->negocios );
			$mapper2->loadHorarios( $this->view->negocios );
			
			
			$this->view->showOrder = true;
		}
		
		$logger->logSearch($what, $where, $this->getRequest()->getClientIp(), $totalItems);
		
		
//		$this->view->limit = $limit;
//		$this->view->page = $page;
//		$this->view->orderBy = $orderBy;
		
        if ( $this->getRequest()->isXmlHttpRequest()){
            $this->_helper->layout->disableLayout();
            $this->render('negocio');
        }
	
	}
	
	public function cadenaAction()
	{
	    $mapper = Delivery_Model_Factory::mapper( 'Negocios' );
	    
	    if( $this->whiteLabelName == 'empanadas-gourmet'){
	        $ids = array(7,8);
	    } else {
	        $ids = array(9,10,11,12,13,14);
	    }
	    
	    
	    $this->view->negocios = $mapper->getByIds( $ids ); 
	    
	    $mapper->loadDireccion( $this->view->negocios );
	    $mapper->loadZonas( $this->view->negocios );
	}

}

